package com.ks.user.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
@Order(2)
public class ErrorLogAspect {

    ThreadLocal<Long> startTime = new ThreadLocal<Long>();

    @Pointcut("execution(public Object com.ks.user.aop.GlobalExceptionHandler.*(..))")
    private void apiLog() {
    }

    @Before("apiLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        startTime.set(System.currentTimeMillis());
    }

    @AfterReturning(returning = "ret", pointcut = "apiLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求，返回内容
        System.out.println("哈哈" + ret);
        log.info("RESPONSE : " + ret);
        log.info("耗时（毫秒） : " + (System.currentTimeMillis() - startTime.get()));
    }
}
